﻿using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;

namespace iTool.Common.Models
{

    public class ExcludeRule
    {
        /// <summary>
        /// 表名称
        /// </summary>
        public string TableName { get; set; }

        /// <summary>
        /// 忽略该表
        /// </summary>
        public bool IsIgnoreTable { get; set; }

        /// <summary>
        /// 忽略数据
        /// </summary>
        public bool IsIgnoreData { get; set; }
    }


    public class DataTableSchema
    {
        // 列名信息
        public Dictionary<string, Column> Columns { get; set; }
        // 索引信息
        public Dictionary<string, List<string>> SearchIndex { get; set; }
        // 默认数据，创建表时添加的数据
        public List<JObject> DefaultData { get; set; }
        //  每次执行都如果有都会插入
        public List<JObject> InsertData { get; set; }
        // 清除数据库所有数据
        public bool ClearData { get; set; }
    }


    public class Column
    {
        [JsonConverter(typeof(StringEnumConverter))]
        public ColumnType Type { get; set; }
        public bool PrimaryKey { get; set; }
        public bool Identity { get; set; }
        public bool IsNotNull { get; set; }
        public int Length { get; set; }
        public int Digits { get; set; }
        public object Default { get; set; }
    }

    public enum ColumnType
    {
        text,
        @char,
        number,
        @decimal,
        @float,
        date,
        location,
        @bool
    }


    public static class ConverExtend
    {
        public static int GetLength(this Column column)
        {
            if (column.Type == ColumnType.location)
            {
                return 50;
            }
            else
            {
                return column.Length;
            }
        }

        public static string GetDefault(this Column column)
        {
            if (column.Type == ColumnType.location)
            {
                return Newtonsoft.Json.JsonConvert.SerializeObject(column.Default);
            }
            else if (column.Type == ColumnType.date)
            {
                return column.Default?.ToString() == "now" ? "getdate()" : column.Default?.ToString();
            }
            else
            {
                return column.Default?.ToString();
            }
        }

        public static string ToType(this ColumnType type)
        {
            string result = string.Empty;
            switch (type)
            {
                case ColumnType.text:
                    result = "nvarchar";
                    break;
                case ColumnType.@char:
                    result = "char";
                    break;
                case ColumnType.number:
                    result = "bigint";
                    break;
                case ColumnType.@decimal:
                    result = "decimal";
                    break;
                case ColumnType.@float:
                    result = "float";
                    break;
                case ColumnType.date:
                    result = "datetime";
                    break;
                case ColumnType.location:
                    result = "varchar";
                    break;
                case ColumnType.@bool:
                    result = "bit";
                    break;
                default:
                    break;
            }
            return result;
        }
    }
}
